﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsAppTest.Dao;
using WinFormsAppTest.Service;

namespace WinFormsAppTest
{
    public partial class ChangeUpdateForm : Form
    {
        public int changeid;
        public string type;
        public DateTime time;
        public int userid;
        public string adminname;
        public int adminid;

        public ChangeUpdateForm()
        {
            InitializeComponent();
        }

        public ChangeUpdateForm(int changeid, string type, DateTime time,
                                int userid, string adminname, int adminid)
        {
            this.changeid = changeid;
            this.type = type;
            this.time = time;
            this.userid = userid;
            this.adminname = adminname;
            this.adminid = adminid;
            InitializeComponent();
        }



        //public ChangeUpdateForm(int uid, string uname, string upassword, string aname, int aid)
        //{
        //    username = uname;
        //    password = upassword;
        //    adminname = aname;
        //    adminid = aid;
        //    userid = uid;
        //    InitializeComponent();
        //}



        private void FilesUpdate_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                // 关闭进程
                Process.GetCurrentProcess().Kill();
            }
        }

        private void FilesUpdate_Load(object sender, EventArgs e)
        {
            //需要加载员工编号和姓名方便人事变动
            //直接调用service层根据员工id取出员工名
            AdminService adminService = new AdminService();

            TypetextBox.Text = type;
            ChangedateTimePicker.Value = time;
            Idlabel.Text = userid.ToString();
            Namelabel.Text = adminService.GetUserById(userid)[0].Name;
        }

        private void Exitbutton_Click(object sender, EventArgs e)
        {
            //this.Hide();
            //ChangeForm f = new ChangeForm(adminname, adminid);
            //f.ShowDialog();
            //this.Dispose();

            if (adminid != 0)
            {
                ChangeForm f2 = new ChangeForm(adminname, adminid);
                this.Hide();
                f2.ShowDialog();
                this.Dispose();
            }
            else
            {
                AdminDao admindao = new AdminDao();
                Form2 f2 = new Form2(admindao.GetUserById(userid)[0].Name, userid);
                this.Hide();
                f2.ShowDialog();
                this.Dispose();
            }
        }

        //private void OKbutton_Click(object sender, EventArgs e)
        //{
        //    if (string.IsNullOrWhiteSpace(MsgtextBox.Text))
        //    {
        //        MessageBox.Show("添加员工基本信息不能为空！");
        //    }
        //    else if (string.IsNullOrWhiteSpace(HousetextBox.Text))
        //    {
        //        MessageBox.Show("添加户口情况不能为空！");
        //    }
        //    else if (string.IsNullOrWhiteSpace(ContracttextBox.Text))
        //    {
        //        MessageBox.Show("添加合同管理不能为空！");
        //    }
        //    else if (string.IsNullOrWhiteSpace(InsuretextBox.Text))
        //    {
        //        MessageBox.Show("添加投保情况不能为空！");
        //    }
        //    else
        //    {
        //        //直接调用dao层进行添加
        //        FilesDao filesdao = new FilesDao();
        //        filesdao.UpdateFilesById(fileid, MsgtextBox.Text, HousetextBox.Text,
        //                                 ContracttextBox.Text, InsuretextBox.Text);
        //        MessageBox.Show("更新成功！");
        //        FilesForm f2 = new FilesForm(adminname, adminid);
        //        this.Hide();
        //        f2.ShowDialog();
        //        this.Dispose();

        //    }
        //}

        private void Okbutton_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrWhiteSpace(TypetextBox.Text))
            {
                MessageBox.Show("更新的变动类型不能为空！");
            }
            else if (ChangedateTimePicker.Value == DateTime.MinValue ||
                     ChangedateTimePicker.Value == null)
            {
                MessageBox.Show("变动时间不能为空！");
            }
            else
            {
                //直接调用dao层进行添加
                ChangeDao changedao = new ChangeDao();
                changedao.UpdateChangeById(changeid, TypetextBox.Text, ChangedateTimePicker.Value);
                //MessageBox.Show("更新成功！" +fileid+ "  "+ MsgtextBox.Text +"  "+ HousetextBox.Text
                //                            +"  "+ ContracttextBox.Text+"  "+ InsuretextBox.Text);
                MessageBox.Show("更新成功！");
                if(adminid != 0)
                {
                    ChangeForm f2 = new ChangeForm(adminname, adminid);
                    this.Hide();
                    f2.ShowDialog();
                    this.Dispose();
                }
                else
                {
                    AdminDao admindao = new AdminDao();
                   Form2 f2 = new Form2(admindao.GetUserById(userid)[0].Name, userid);
                    this.Hide();
                    f2.ShowDialog();
                    this.Dispose();
                }

            }
        }
    }
}
